home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / spreadsheet / service.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  19.3 KB  |  436 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''SpreadsheetsService extends the GDataService to streamline Google
  5. Spreadsheets operations.
  6.  
  7.   GBaseService: Provides methods to query feeds and manipulate items. Extends
  8.                 GDataService.
  9.  
  10.   DictionaryToParamList: Function which converts a dictionary into a list of
  11.                          URL arguments (represented as strings). This is a
  12.                          utility function used in CRUD operations.
  13. '''
  14. __author__ = 'api.laurabeth@gmail.com (Laura Beth Lincoln)'
  15. import gdata
  16. import atom.service as atom
  17. import gdata.service as gdata
  18. import gdata.spreadsheet as gdata
  19. import atom
  20.  
  21. class Error(Exception):
  22.     '''Base class for exceptions in this module.'''
  23.     pass
  24.  
  25.  
  26. class RequestError(Error):
  27.     pass
  28.  
  29.  
  30. class SpreadsheetsService(gdata.service.GDataService):
  31.     '''Client for the Google Spreadsheets service.'''
  32.     
  33.     def __init__(self, email = None, password = None, source = None, server = 'spreadsheets.google.com', additional_headers = None, **kwargs):
  34.         """Creates a client for the Google Spreadsheets service.
  35.  
  36.     Args:
  37.       email: string (optional) The user's email address, used for
  38.           authentication.
  39.       password: string (optional) The user's password.
  40.       source: string (optional) The name of the user's application.
  41.       server: string (optional) The name of the server to which a connection
  42.           will be opened. Default value: 'spreadsheets.google.com'.
  43.       **kwargs: The other parameters to pass to gdata.service.GDataService
  44.           constructor.
  45.     """
  46.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'wise', source = source, server = server, additional_headers = additional_headers, **kwargs)
  47.  
  48.     
  49.     def GetSpreadsheetsFeed(self, key = None, query = None, visibility = 'private', projection = 'full'):
  50.         '''Gets a spreadsheets feed or a specific entry if a key is defined
  51.     Args:
  52.       key: string (optional) The spreadsheet key defined in /ccc?key=
  53.       query: DocumentQuery (optional) Query parameters
  54.       
  55.     Returns:
  56.       If there is no key, then a SpreadsheetsSpreadsheetsFeed.
  57.       If there is a key, then a SpreadsheetsSpreadsheet.
  58.     '''
  59.         uri = 'http://%s/feeds/spreadsheets/%s/%s' % (self.server, visibility, projection)
  60.         if key is not None:
  61.             uri = '%s/%s' % (uri, key)
  62.         
  63.         if query != None:
  64.             query.feed = uri
  65.             uri = query.ToUri()
  66.         
  67.         if key:
  68.             return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsSpreadsheetFromString)
  69.         return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsSpreadsheetsFeedFromString)
  70.  
  71.     
  72.     def GetWorksheetsFeed(self, key, wksht_id = None, query = None, visibility = 'private', projection = 'full'):
  73.         '''Gets a worksheets feed or a specific entry if a wksht is defined
  74.     Args:
  75.       key: string The spreadsheet key defined in /ccc?key=
  76.       wksht_id: string (optional) The id for a specific worksheet entry
  77.       query: DocumentQuery (optional) Query parameters
  78.       
  79.     Returns:
  80.       If there is no wksht_id, then a SpreadsheetsWorksheetsFeed.
  81.       If there is a wksht_id, then a SpreadsheetsWorksheet.
  82.     '''
  83.         uri = 'http://%s/feeds/worksheets/%s/%s/%s' % (self.server, key, visibility, projection)
  84.         if wksht_id != None:
  85.             uri = '%s/%s' % (uri, wksht_id)
  86.         
  87.         if query != None:
  88.             query.feed = uri
  89.             uri = query.ToUri()
  90.         
  91.         if wksht_id:
  92.             return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  93.         return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsWorksheetsFeedFromString)
  94.  
  95.     
  96.     def AddWorksheet(self, title, row_count, col_count, key):
  97.         '''Creates a new worksheet in the desired spreadsheet.
  98.  
  99.     The new worksheet is appended to the end of the list of worksheets. The
  100.     new worksheet will only have the available number of columns and cells 
  101.     specified.
  102.  
  103.     Args:
  104.       title: str The title which will be displayed in the list of worksheets.
  105.       row_count: int or str The number of rows in the new worksheet.
  106.       col_count: int or str The number of columns in the new worksheet.
  107.       key: str The spreadsheet key to the spreadsheet to which the new 
  108.           worksheet should be added. 
  109.  
  110.     Returns:
  111.       A SpreadsheetsWorksheet if the new worksheet was created succesfully.  
  112.     '''
  113.         new_worksheet = gdata.spreadsheet.SpreadsheetsWorksheet(title = atom.Title(text = title), row_count = gdata.spreadsheet.RowCount(text = str(row_count)), col_count = gdata.spreadsheet.ColCount(text = str(col_count)))
  114.         return self.Post(new_worksheet, 'http://%s/feeds/worksheets/%s/private/full' % (self.server, key), converter = gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  115.  
  116.     
  117.     def UpdateWorksheet(self, worksheet_entry, url = None):
  118.         '''Changes the size and/or title of the desired worksheet.
  119.     
  120.     Args:
  121.       worksheet_entry: SpreadsheetWorksheet The new contents of the 
  122.           worksheet. 
  123.       url: str (optional) The URL to which the edited worksheet entry should
  124.           be sent. If the url is None, the edit URL from the worksheet will
  125.           be used.
  126.  
  127.     Returns: 
  128.       A SpreadsheetsWorksheet with the new information about the worksheet.
  129.     '''
  130.         if not url:
  131.             pass
  132.         target_url = worksheet_entry.GetEditLink().href
  133.         return self.Put(worksheet_entry, target_url, converter = gdata.spreadsheet.SpreadsheetsWorksheetFromString)
  134.  
  135.     
  136.     def DeleteWorksheet(self, worksheet_entry = None, url = None):
  137.         """Removes the desired worksheet from the spreadsheet
  138.     
  139.     Args:
  140.       worksheet_entry: SpreadsheetWorksheet (optional) The worksheet to
  141.           be deleted. If this is none, then the DELETE reqest is sent to 
  142.           the url specified in the url parameter.
  143.       url: str (optaional) The URL to which the DELETE request should be
  144.           sent. If left as None, the worksheet's edit URL is used.
  145.  
  146.     Returns:
  147.       True if the worksheet was deleted successfully. 
  148.     """
  149.         if url:
  150.             target_url = url
  151.         else:
  152.             target_url = worksheet_entry.GetEditLink().href
  153.         return self.Delete(target_url)
  154.  
  155.     
  156.     def GetCellsFeed(self, key, wksht_id = 'default', cell = None, query = None, visibility = 'private', projection = 'full'):
  157.         '''Gets a cells feed or a specific entry if a cell is defined
  158.     Args:
  159.       key: string The spreadsheet key defined in /ccc?key=
  160.       wksht_id: string The id for a specific worksheet entry
  161.       cell: string (optional) The R1C1 address of the cell
  162.       query: DocumentQuery (optional) Query parameters
  163.       
  164.     Returns:
  165.       If there is no cell, then a SpreadsheetsCellsFeed.
  166.       If there is a cell, then a SpreadsheetsCell.
  167.     '''
  168.         uri = 'http://%s/feeds/cells/%s/%s/%s/%s' % (self.server, key, wksht_id, visibility, projection)
  169.         if cell != None:
  170.             uri = '%s/%s' % (uri, cell)
  171.         
  172.         if query != None:
  173.             query.feed = uri
  174.             uri = query.ToUri()
  175.         
  176.         if cell:
  177.             return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsCellFromString)
  178.         return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsCellsFeedFromString)
  179.  
  180.     
  181.     def GetListFeed(self, key, wksht_id = 'default', row_id = None, query = None, visibility = 'private', projection = 'full'):
  182.         '''Gets a list feed or a specific entry if a row_id is defined
  183.     Args:
  184.       key: string The spreadsheet key defined in /ccc?key=
  185.       wksht_id: string The id for a specific worksheet entry
  186.       row_id: string (optional) The row_id of a row in the list
  187.       query: DocumentQuery (optional) Query parameters
  188.       
  189.     Returns:
  190.       If there is no row_id, then a SpreadsheetsListFeed.
  191.       If there is a row_id, then a SpreadsheetsList.
  192.     '''
  193.         uri = 'http://%s/feeds/list/%s/%s/%s/%s' % (self.server, key, wksht_id, visibility, projection)
  194.         if row_id is not None:
  195.             uri = '%s/%s' % (uri, row_id)
  196.         
  197.         if query is not None:
  198.             query.feed = uri
  199.             uri = query.ToUri()
  200.         
  201.         if row_id:
  202.             return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsListFromString)
  203.         return self.Get(uri, converter = gdata.spreadsheet.SpreadsheetsListFeedFromString)
  204.  
  205.     
  206.     def UpdateCell(self, row, col, inputValue, key, wksht_id = 'default'):
  207.         '''Updates an existing cell.
  208.     
  209.     Args:
  210.       row: int The row the cell to be editted is in
  211.       col: int The column the cell to be editted is in
  212.       inputValue: str the new value of the cell
  213.       key: str The key of the spreadsheet in which this cell resides.
  214.       wksht_id: str The ID of the worksheet which holds this cell.
  215.       
  216.     Returns:
  217.       The updated cell entry
  218.     '''
  219.         row = str(row)
  220.         col = str(col)
  221.         new_cell = gdata.spreadsheet.Cell(row = row, col = col, inputValue = inputValue)
  222.         cell = 'R%sC%s' % (row, col)
  223.         entry = self.GetCellsFeed(key, wksht_id, cell)
  224.         for a_link in entry.link:
  225.             if a_link.rel == 'edit':
  226.                 entry.cell = new_cell
  227.                 return self.Put(entry, a_link.href, converter = gdata.spreadsheet.SpreadsheetsCellFromString)
  228.         
  229.  
  230.     
  231.     def _GenerateCellsBatchUrl(self, spreadsheet_key, worksheet_id):
  232.         return 'http://spreadsheets.google.com/feeds/cells/%s/%s/private/full/batch' % (spreadsheet_key, worksheet_id)
  233.  
  234.     
  235.     def ExecuteBatch(self, batch_feed, url = None, spreadsheet_key = None, worksheet_id = None, converter = gdata.spreadsheet.SpreadsheetsCellsFeedFromString):
  236.         """Sends a batch request feed to the server.
  237.  
  238.     The batch request needs to be sent to the batch URL for a particular 
  239.     worksheet. You can specify the worksheet by providing the spreadsheet_key
  240.     and worksheet_id, or by sending the URL from the cells feed's batch link.
  241.  
  242.     Args:
  243.       batch_feed: gdata.spreadsheet.SpreadsheetsCellFeed A feed containing 
  244.           BatchEntry elements which contain the desired CRUD operation and 
  245.           any necessary data to modify a cell.
  246.       url: str (optional) The batch URL for the cells feed to which these 
  247.           changes should be applied. This can be found by calling 
  248.           cells_feed.GetBatchLink().href.
  249.       spreadsheet_key: str (optional) Used to generate the batch request URL
  250.           if the url argument is None. If using the spreadsheet key to 
  251.           generate the URL, the worksheet id is also required.
  252.       worksheet_id: str (optional) Used if the url is not provided, it is 
  253.           oart of the batch feed target URL. This is used with the spreadsheet
  254.           key.
  255.       converter: Function (optional) Function to be executed on the server's
  256.           response. This function should take one string as a parameter. The
  257.           default value is SpreadsheetsCellsFeedFromString which will turn the result
  258.           into a gdata.base.GBaseItem object.
  259.  
  260.     Returns:
  261.       A gdata.BatchFeed containing the results.
  262.     """
  263.         if url is None:
  264.             url = self._GenerateCellsBatchUrl(spreadsheet_key, worksheet_id)
  265.         
  266.         return self.Post(batch_feed, url, converter = converter)
  267.  
  268.     
  269.     def InsertRow(self, row_data, key, wksht_id = 'default'):
  270.         '''Inserts a new row with the provided data
  271.     
  272.     Args:
  273.       uri: string The post uri of the list feed
  274.       row_data: dict A dictionary of column header to row data
  275.     
  276.     Returns:
  277.       The inserted row
  278.     '''
  279.         new_entry = gdata.spreadsheet.SpreadsheetsList()
  280.         for k, v in row_data.iteritems():
  281.             new_custom = gdata.spreadsheet.Custom()
  282.             new_custom.column = k
  283.             new_custom.text = v
  284.             new_entry.custom[new_custom.column] = new_custom
  285.         
  286.         post_url = 'http://spreadsheets.google.com/feeds/list/%s/%s/private/full' % (key, wksht_id)
  287.         return self.Post(new_entry, post_url, converter = gdata.spreadsheet.SpreadsheetsListFromString)
  288.  
  289.     
  290.     def UpdateRow(self, entry, new_row_data):
  291.         '''Updates a row with the provided data
  292.     
  293.     Args:
  294.       entry: gdata.spreadsheet.SpreadsheetsList The entry to be updated
  295.       new_row_data: dict A dictionary of column header to row data
  296.       
  297.     Returns:
  298.       The updated row
  299.     '''
  300.         entry.custom = { }
  301.         for k, v in new_row_data.iteritems():
  302.             new_custom = gdata.spreadsheet.Custom()
  303.             new_custom.column = k
  304.             new_custom.text = v
  305.             entry.custom[k] = new_custom
  306.         
  307.         for a_link in entry.link:
  308.             if a_link.rel == 'edit':
  309.                 return self.Put(entry, a_link.href, converter = gdata.spreadsheet.SpreadsheetsListFromString)
  310.         
  311.  
  312.     
  313.     def DeleteRow(self, entry):
  314.         '''Deletes a row, the provided entry
  315.     
  316.     Args:
  317.       entry: gdata.spreadsheet.SpreadsheetsList The row to be deleted
  318.     
  319.     Returns:
  320.       The delete response
  321.     '''
  322.         for a_link in entry.link:
  323.             if a_link.rel == 'edit':
  324.                 return self.Delete(a_link.href)
  325.         
  326.  
  327.  
  328.  
  329. class DocumentQuery(gdata.service.Query):
  330.     
  331.     def _GetTitleQuery(self):
  332.         return self['title']
  333.  
  334.     
  335.     def _SetTitleQuery(self, document_query):
  336.         self['title'] = document_query
  337.  
  338.     title = property(_GetTitleQuery, _SetTitleQuery, doc = 'The title query parameter')
  339.     
  340.     def _GetTitleExactQuery(self):
  341.         return self['title-exact']
  342.  
  343.     
  344.     def _SetTitleExactQuery(self, document_query):
  345.         self['title-exact'] = document_query
  346.  
  347.     title_exact = property(_GetTitleExactQuery, _SetTitleExactQuery, doc = 'The title-exact query parameter')
  348.  
  349.  
  350. class CellQuery(gdata.service.Query):
  351.     
  352.     def _GetMinRowQuery(self):
  353.         return self['min-row']
  354.  
  355.     
  356.     def _SetMinRowQuery(self, cell_query):
  357.         self['min-row'] = cell_query
  358.  
  359.     min_row = property(_GetMinRowQuery, _SetMinRowQuery, doc = 'The min-row query parameter')
  360.     
  361.     def _GetMaxRowQuery(self):
  362.         return self['max-row']
  363.  
  364.     
  365.     def _SetMaxRowQuery(self, cell_query):
  366.         self['max-row'] = cell_query
  367.  
  368.     max_row = property(_GetMaxRowQuery, _SetMaxRowQuery, doc = 'The max-row query parameter')
  369.     
  370.     def _GetMinColQuery(self):
  371.         return self['min-col']
  372.  
  373.     
  374.     def _SetMinColQuery(self, cell_query):
  375.         self['min-col'] = cell_query
  376.  
  377.     min_col = property(_GetMinColQuery, _SetMinColQuery, doc = 'The min-col query parameter')
  378.     
  379.     def _GetMaxColQuery(self):
  380.         return self['max-col']
  381.  
  382.     
  383.     def _SetMaxColQuery(self, cell_query):
  384.         self['max-col'] = cell_query
  385.  
  386.     max_col = property(_GetMaxColQuery, _SetMaxColQuery, doc = 'The max-col query parameter')
  387.     
  388.     def _GetRangeQuery(self):
  389.         return self['range']
  390.  
  391.     
  392.     def _SetRangeQuery(self, cell_query):
  393.         self['range'] = cell_query
  394.  
  395.     range = property(_GetRangeQuery, _SetRangeQuery, doc = 'The range query parameter')
  396.     
  397.     def _GetReturnEmptyQuery(self):
  398.         return self['return-empty']
  399.  
  400.     
  401.     def _SetReturnEmptyQuery(self, cell_query):
  402.         self['return-empty'] = cell_query
  403.  
  404.     return_empty = property(_GetReturnEmptyQuery, _SetReturnEmptyQuery, doc = 'The return-empty query parameter')
  405.  
  406.  
  407. class ListQuery(gdata.service.Query):
  408.     
  409.     def _GetSpreadsheetQuery(self):
  410.         return self['sq']
  411.  
  412.     
  413.     def _SetSpreadsheetQuery(self, list_query):
  414.         self['sq'] = list_query
  415.  
  416.     sq = property(_GetSpreadsheetQuery, _SetSpreadsheetQuery, doc = 'The sq query parameter')
  417.     
  418.     def _GetOrderByQuery(self):
  419.         return self['orderby']
  420.  
  421.     
  422.     def _SetOrderByQuery(self, list_query):
  423.         self['orderby'] = list_query
  424.  
  425.     orderby = property(_GetOrderByQuery, _SetOrderByQuery, doc = 'The orderby query parameter')
  426.     
  427.     def _GetReverseQuery(self):
  428.         return self['reverse']
  429.  
  430.     
  431.     def _SetReverseQuery(self, list_query):
  432.         self['reverse'] = list_query
  433.  
  434.     reverse = property(_GetReverseQuery, _SetReverseQuery, doc = 'The reverse query parameter')
  435.  
  436.